package kr.co.casually;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;

import kr.co.casually.adapter.ConcertDbAdapter;
import kr.co.casually.adapter.SubjCodeArtListAdapter;
import kr.co.casually.dialog.AppCloseDialog;
import kr.co.casually.dialog.SendDialog;
import kr.co.casually.types.ConcertData;
import kr.co.casually.util.Utils;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;

public class ConcertActivity extends Activity {

	private ListView mCategory_listView = null;
	private ArrayAdapter<CharSequence> mCategory_spinner_adapter = null;
	private SubjCodeArtListAdapter mSubjCodeArtListAdapter;
	private ConcertDbAdapter concerDb = null;
	private ArrayList<ConcertData> mSubjCodeArtList = null;
	private ArrayList<Integer> mCultCode = null;
	private LinearLayout mCateSearchLinear = null;
	private boolean mCateSearchLinear_flag = false;
	private EditText mCategory_search_edittext = null;
	private String mSearchText = "";

	int mGenreCode = 0;
	String mGenreTitle = "";

	private int[] mCategoryCode = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
			13, 14, 15, 16 };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.art_cate_list);

		mCategory_listView = (ListView) findViewById(R.id.category_listView);
		mCategory_listView.setFastScrollEnabled(true);

		Spinner mCategory_spinner = (Spinner) findViewById(R.id.category_spinner);
		mCategory_search_edittext = (EditText) findViewById(R.id.category_search_edittext);
		ImageView mCategory_search_button = (ImageView) findViewById(R.id.category_search_button);
		ImageView mCategory_search_ImgBtn = (ImageView) findViewById(R.id.category_search_imgbutton);
		mCateSearchLinear = (LinearLayout) findViewById(R.id.category_search_linear);

		if (mCateSearchLinear_flag == false) {
			mCateSearchLinear.setVisibility(View.GONE);
			mCateSearchLinear_flag = true;
		}

		// WaitDlg dlg = new WaitDlg(ConcertActivity.this, null,
		// Utils.DIALOG_MSG);
		// dlg.start();

		mSubjCodeArtList = new ArrayList<ConcertData>();
		mCultCode = new ArrayList<Integer>();

		mCategory_spinner_adapter = ArrayAdapter.createFromResource(this,
				R.array.genre_name, android.R.layout.simple_spinner_item);
		mCategory_spinner_adapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mCategory_spinner.setAdapter(mCategory_spinner_adapter);

		mSubjCodeArtListAdapter = new SubjCodeArtListAdapter(this,
				mSubjCodeArtList);
		mCategory_listView.setAdapter(mSubjCodeArtListAdapter);

		mCategory_listView.setOnItemClickListener(categoryClick);
		mCategory_listView.setOnItemLongClickListener(mItemLongClickListener);
		mCategory_spinner.setOnItemSelectedListener(cateClick);
		mCategory_search_ImgBtn.setOnClickListener(seachClick);
		mCategory_search_button.setOnClickListener(seachClick);

		// WaitDlg.stop(dlg);

	}

	// 검색버튼을 누를때
	OnClickListener seachClick = new OnClickListener() {

		@Override
		public void onClick(View v) {

			switch (v.getId()) {
			case R.id.category_search_imgbutton:

				if (mCateSearchLinear_flag == false) {
					mCateSearchLinear.setVisibility(View.GONE);
					mCateSearchLinear_flag = true;
				} else {
					mCateSearchLinear.setVisibility(View.VISIBLE);
					mCateSearchLinear_flag = false;
				}

				break;

			case R.id.category_search_button:
				mSearchText = mCategory_search_edittext.getText().toString();
				getSubjCodeArtListData(mGenreCode, mSearchText);

				break;
			}

		}
	};

	// 스피너 아이템을 클릭
	OnItemSelectedListener cateClick = new OnItemSelectedListener() {

		@Override
		public void onItemSelected(AdapterView<?> parent, View view,
				int position, long id) {

			mGenreCode = mCategoryCode[position];
			mSearchText = mCategory_search_edittext.getText().toString();

			getSubjCodeArtListData(mGenreCode, mSearchText);

		}

		@Override
		public void onNothingSelected(AdapterView<?> parent) {
		}
	};

	// 리스트 아이템을 선택
	OnItemClickListener categoryClick = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View view, int position,
				long arg3) {

			Intent ArtInfo = new Intent(getApplicationContext(), ArtInfo.class);
			ArtInfo.putExtra("cultCode", mCultCode.get(position));
			startActivity(ArtInfo);
		}
	};

	// 검색에 의해 공연리스트를 가져옴
	public void getSubjCodeArtListData(int cate_genre_code,
			String cate_genre_title) {

		String where = "";

		if (cate_genre_code == 0) {
			where = " WHERE 1 ";

			if (!Utils.isEmpty(mSearchText)) { // 전체에서 검색
				where += " AND ";
				where += " title like '%" + cate_genre_title + "%'";
			}

		} else {

			if (Utils.isEmpty(mSearchText)) { // 카테고리에서 검색창이 null 일때

				where = " WHERE  1";
				where += " AND ";
				where += " genrecode=" + cate_genre_code;

			} else { // 카테고리에서 검색어가 있을때
				where = " WHERE  ";
				where += " genrecode=" + cate_genre_code;
				where += " AND ";
				where += " title like '%" + cate_genre_title + "%'";

			}

		}

		// where += " AND ";
		// where += " genrecode=" + cate_genre_code;

		if (mCultCode.size() > 0) {
			mCultCode.clear();
		}
		mSubjCodeArtList.clear();
		concerDb = new ConcertDbAdapter(this);
		concerDb.open();
		Cursor subjCodeArtList = concerDb.selectMyConcertList(where);
		while (subjCodeArtList.moveToNext()) {
			ConcertData concertData = new ConcertData();

			int cultCode = subjCodeArtList.getInt(1);
			int genreCode = subjCodeArtList.getInt(2);
			String subjName = subjCodeArtList.getString(3);
			String title = subjCodeArtList.getString(4);
			String startDate = subjCodeArtList.getString(5);
			String endDate = subjCodeArtList.getString(6);
			String place = subjCodeArtList.getString(7);
			String imagePath = subjCodeArtList.getString(8);

			String imageName = imagePath
					.substring(imagePath.lastIndexOf("/") + 1);

			// 한글 주소 인코딩
			try {
				imageName = URLEncoder.encode(imageName, "euc-kr");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}

			concertData.setCultCode(cultCode);
			concertData.setGenreCode(genreCode);
			concertData.setTitle(title);
			concertData.setSubjName(subjName);
			concertData.setStartDate(startDate);
			concertData.setEndDate(endDate);
			concertData.setPlace(place);
			concertData.setImagepath(imagePath);

			mSubjCodeArtList.add(concertData);
			mSubjCodeArtListAdapter.notifyDataSetChanged();
			mCultCode.add(cultCode);

		}
		subjCodeArtList.close();
		concerDb.close();

		mSubjCodeArtListAdapter.notifyDataSetChanged();

	}

	// 전부 다가져옴
	public void getAllArtList() {
		if (mCultCode.size() > 0) {
			mCultCode.clear();
		}

		mSubjCodeArtList.clear();
		concerDb = new ConcertDbAdapter(this);
		concerDb.open();
		Cursor allArtList = concerDb.fetchAllConcert();
		while (allArtList.moveToNext()) {
			ConcertData concertData = new ConcertData();

			int cultCode = allArtList.getInt(1);
			int genreCode = allArtList.getInt(2);
			String subjName = allArtList.getString(3);
			String title = allArtList.getString(4);
			String startDate = allArtList.getString(5);
			String endDate = allArtList.getString(6);
			String place = allArtList.getString(7);
			String imagePath = allArtList.getString(8);

			String path = imagePath
					.substring(0, imagePath.lastIndexOf("/") + 1);
			String imageName = imagePath
					.substring(imagePath.lastIndexOf("/") + 1);

			// 한글 주소 인코딩
			try {
				imageName = URLEncoder.encode(imageName, "euc-kr");

			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			String imgUrl = path + imageName;

			concertData.setCultCode(cultCode);
			concertData.setGenreCode(genreCode);
			concertData.setTitle(title);
			concertData.setSubjName(subjName);
			concertData.setStartDate(startDate);
			concertData.setEndDate(endDate);
			concertData.setPlace(place);
			concertData.setImagepath(imgUrl);

			mSubjCodeArtList.add(concertData);
			mSubjCodeArtListAdapter.notifyDataSetChanged();
			mCultCode.add(cultCode);

		}

		allArtList.close();
		concerDb.close();

		mSubjCodeArtListAdapter.notifyDataSetChanged();
	}

	OnItemLongClickListener mItemLongClickListener = new OnItemLongClickListener() {

		@Override
		public boolean onItemLongClick(AdapterView<?> arg0, View v,
				final int pos, long id) {
			Dialog sendDialog = new SendDialog(ConcertActivity.this,
					ConcertActivity.this, mSubjCodeArtList.get(pos));
			sendDialog.show();
			return false;
		}

	};

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Dialog dialog = new AppCloseDialog(this, this);
			dialog.show();

		}
		return true;
	}
}
